package com.aliyun.oss.userController;

import com.aliyun.oss.UserService.OrdersService;
import com.aliyun.oss.pojo.DTO.OrdersPaymentDTO;
import com.aliyun.oss.pojo.DTO.OrdersSubmitDTO;
import com.aliyun.oss.pojo.Entity.Orders;
import com.aliyun.oss.pojo.Vo.AllOrdersVO;
import com.aliyun.oss.pojo.Vo.OrderPaymentVO;
import com.aliyun.oss.pojo.Vo.OrderSubmitVO;
import com.aliyun.oss.pojo.Vo.ResultVO;
import com.aliyun.oss.websocket.WebSocketServer;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController("userOrderController")
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    OrdersService ordersService;


    @PostMapping("/submit")
    public ResultVO submit(@RequestAttribute("id") Integer id, @RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        OrderSubmitVO orderSubmitVO = ordersService.submitOrder(id, ordersSubmitDTO);
        return ResultVO.success(orderSubmitVO);
    }
//    订单支付
    @PatchMapping("/payment")
    public ResultVO payment(@RequestAttribute("id") Integer id, @RequestBody OrdersPaymentDTO ordersPaymentDTO) {
        System.out.println(ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO= ordersService.payment(id, ordersPaymentDTO);

        return ResultVO.success(orderPaymentVO);
    }
//    用户催单
    @GetMapping("/reminder/{id}")
    public ResultVO reminder(@PathVariable("id") Integer id) throws JsonProcessingException {
        Orders orders = ordersService.getById(id);
        if (orders == null) {
            return ResultVO.error("订单不存在");
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("orderId", id);
        hashMap.put("orderName", orders.getNumber());
        hashMap.put("type", 2);
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(hashMap);
        WebSocketServer.sendMessage(jsonString);
          return ResultVO.success();
    }
//    查询所有订单
    @GetMapping("/list")
    public ResultVO list(@RequestAttribute("id") Integer id) {
        List<AllOrdersVO> allOrdersVOList= ordersService.getAllOrders(id);
        return ResultVO.success(allOrdersVOList);
    }
}
